package com.tf_cut.backend.agent.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tf_cut.backend.agent.pojo.AgentAccountDetail;
import com.tf_cut.backend.agent.repository.AccountDetailRepository;
import com.tf_cut.backend.agent.vo.AgentAccountDetailVo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.cpm.pojo.CpmInsideTrans;
import com.tf_cut.backend.cpm.repository.CpmInsideTransRepository;
import com.tf_cut.backend.util.CalendarUtil;
import com.tf_cut.backend.util.UUIDUtil;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class AccountDetailServiceImpl extends CommonServiceImpl<AgentAccountDetailVo, AgentAccountDetail, String> implements AccountDetailService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private AccountDetailRepository accountDetailRepository;
    
    @Autowired
    private CpmInsideTransRepository  cpmInsideTransRepository;
    
    @Override
    public Result<Boolean> saveOperation(AgentAccountDetailVo vo){
    	Result<Boolean> result = Result.of(true);
    	try {
    		Result<AgentAccountDetailVo> save = super.save(vo); // 加/减款写入明细
    		
	    	CpmInsideTrans cpmInsideTrans = new CpmInsideTrans();
	    	// 自定义
        	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	String endDate = df.format(new Date());
        	String year = endDate.substring(0, 4);
            String month = endDate.substring(5,7);
            String day = endDate.substring(8,10);
            int num = (int)(Math.random()*99+1);
	    	if(vo.getOperType() == 1){
	    		cpmInsideTrans.setSerial("JS"+year+month+day+"0000000"+String.valueOf(num));
	    		cpmInsideTrans.setSubCode("0002");
	    		cpmInsideTrans.setSubType(0);
	    		cpmInsideTrans.setTransAmount(vo.getTradeMoney());
	    		cpmInsideTrans.setDirection(1);
	    		cpmInsideTrans.setVirtualAccount(vo.getAgentAccountId());
	    		cpmInsideTrans.setAccountType(1);
	    		cpmInsideTrans.setCreateTime(Timestamp.valueOf(df.format(new Date())));
	    		cpmInsideTrans.setUpdateTime(Timestamp.valueOf(df.format(new Date())));
	    		cpmInsideTrans.setComment(vo.getTradeComment());
	    		cpmInsideTrans.setIsSyn("Y");
	    	}else{
	    		cpmInsideTrans.setSerial("JS"+year+month+day+"0000000"+String.valueOf(num));
	    		cpmInsideTrans.setSubCode("0001");
	    		cpmInsideTrans.setSubType(0);
	    		cpmInsideTrans.setTransAmount(vo.getTradeMoney());
	    		cpmInsideTrans.setDirection(-1);
	    		cpmInsideTrans.setVirtualAccount(vo.getAgentAccountId());
	    		cpmInsideTrans.setAccountType(1);
	    		cpmInsideTrans.setCreateTime(Timestamp.valueOf(df.format(new Date())));
	    		cpmInsideTrans.setUpdateTime(Timestamp.valueOf(df.format(new Date())));
	    		cpmInsideTrans.setComment(vo.getTradeComment());
	    		cpmInsideTrans.setIsSyn("Y");
	    	}
	    	cpmInsideTrans = cpmInsideTransRepository.save(cpmInsideTrans);
	    		// 将明细更新到虚拟账号，并存档日志
		    	Query q = em.createNativeQuery("{call agent_account_detail_procedure()}");
		    	q.executeUpdate();
    	}catch (Exception e) {
    		result = Result.of(false);
    		result.setMsg(e.getMessage());
    		log.error(e.getMessage(), e);
		}
    	return result;
    }
}
